package com.jdk.learn.collection;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * Created by baiguantao on 2017/12/22.
 */
public class LinkListTest {
    public static void main(String[] args) {
        List<String> a = new LinkedList<>();
        a.add("a");
        a.add("a2");
        a.add("a3");
        List<String> b = new LinkedList<>();
        b.add("b");
        b.add("b1");
        b.add("b2");
        b.add("b3");

        ListIterator<String> al = a.listIterator();
        Iterator<String> bi = b.iterator();
        System.out.println("-----------整合b集合中的元素到a集合中----------");
        while (bi.hasNext()) {//如果b集合还有下一个元素  接着走
            if (al.hasNext()) al.next();//取出a集合的当前元素
            System.out.println(a.size());
            al.add(bi.next());
        }
        System.out.println(a);
        System.out.println("-----------------------------------");

        // remove every second word from b   移除b中每一个第二个元素  即移除b1 b3
        bi = b.iterator();//重置下标索引

        while (bi.hasNext()) {
            bi.next();//skip one element
            if (bi.hasNext()) {
                bi.next();//skip next elenent
                bi.remove();//remove that element
            }
        }
        System.out.println(b);

        //bulk operation:remove all words in b from a
        a.removeAll(b);
        System.out.println(a);


    }
}
